package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.services.CartService;
import org.darkforest.common.services.GoodSearchService;
import org.darkforest.dao.mapper.CartMapper;
import org.darkforest.dao.mapper.GoodsMapper;
import org.darkforest.model.Cart;
import org.darkforest.model.Goods;
import org.darkforest.model.Member;
import org.darkforest.model.RestEntity;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 购物车控制层
 *
 * @auth bingo
 * @create 2017-10-17-14:37
 */
@RestController
@RequestMapping(value = "cart")
@Api(value = "cart", description = "购物车",produces= MediaType.APPLICATION_JSON_VALUE)
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CartMapper cartMapper;
    @AuthPassport
    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ApiOperation(value = "获取购物车商品列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity list(@CurrentMember Member member, String selected) {
        List<Cart> cartList = cartService.findCartList(member.getMemberId() + "", StringUtils.isBlank(selected) ? null : selected, null);
        return RestEntity.success(cartList);
    }

    @AuthPassport
    @RequestMapping(value = "delete",method = RequestMethod.GET)
    @ApiOperation(value = "根据ID删除购物车商品", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity delete(@CurrentMember Member member,  @RequestParam(required = true) String cartIds) {
        if (StringUtils.isBlank(cartIds)) {
            return RestEntity.failed("invalid parameter");
        } else {
            cartService.deleteBatch(member, cartIds);
            return RestEntity.success();
        }
    }

    @AuthPassport
    @RequestMapping(value = "update",method = RequestMethod.GET)
    @ApiOperation(value = "更新购物车商品", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity update(  @RequestParam(required = true ,defaultValue = "")  String cartId,   @RequestParam(required = true)  String qty,   @RequestParam(required = true) String selected) {

        String[] ids = cartId.split(",");
        if(ids.length<1){
            return RestEntity.failed("请选择购物车商品");

        }
        for(String id :ids){
            Cart cart = new Cart();
            cart.setCartId(Integer.parseInt(id));
            cart.setGoodsNum(StringUtils.isBlank(qty) ? null : Short.parseShort(qty));
            cart.setSelected(StringUtils.isBlank(selected) ? null : selected);
            cartService.updateCartGoodsNum(cart);
        }

        return RestEntity.success();
    }


    @AuthPassport
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "添加购物车商品", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity add(@CurrentMember Member member,  @RequestParam(required = true) Integer goodsId, @RequestParam(required = false, defaultValue = "1") short qty) {

        String userId =member.getMemberId()+"";
        if(goodsId==null  || "".equals(userId)){
            return  RestEntity.failed("参数有误");
        }
        Goods goodsInfo = goodsMapper.selectByPrimaryKey(goodsId);

        if(goodsInfo.getGoodsStorage() < qty){
            return  RestEntity.failed("库存不足");
        }

        if(goodsInfo.getGoodsState() !=1){
            return  RestEntity.failed("已下架");
        }

        try{
            //判断该类型的商品是否已经在购物车中
            Cart cartCondition = new Cart();
            cartCondition.setGoodsId(goodsInfo.getGoodsId());
            cartCondition.setBuyerId(userId + "");
            cartCondition.setChooseGoodsSpec(goodsInfo.getGoodsSpec());
            List<Cart> carts = cartMapper.findCartGoodsByCondition(cartCondition);
            if(carts.size()>0){
                //更新购物车里同种商品的数量
                Cart cart = carts.get(0);
                cart.setGoodsNum((short)(carts.get(0).getGoodsNum()+qty));
                cartMapper.updateByPrimaryKey(cart);
            }else{
                Cart cartAdd = new Cart();
                cartAdd.setGoodsCommonid(goodsInfo.getGoodsCommonid());
                cartAdd.setGoodsNum(qty);
                cartAdd.setBuyerId(userId+"");
                cartAdd.setGoodsImage(goodsInfo.getGoodsImage());
                cartAdd.setGoodsId(goodsInfo.getGoodsId());
                cartAdd.setGoodsName(goodsInfo.getGoodsName());
                cartAdd.setBlId(0);
                cartAdd.setGoodsPrice(goodsInfo.getGoodsPrice());
                cartAdd.setStoreId(goodsInfo.getStoreId());
                cartAdd.setStoreName(goodsInfo.getStoreName());
                cartAdd.setGoodsClass(goodsInfo.getGcId().toString());
                cartAdd.setChooseGoodsSpec(goodsInfo.getGoodsSpec());
                cartAdd.setSelected("1");
                cartMapper.insert(cartAdd);
            }

            return  RestEntity.success("添加成功");
        }catch (Exception e){
            return  RestEntity.failed("添加失败");
        }



    }

}
